package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public Result<String> addClassification(CategoryDTO categoryDTO) {
        //非空判断
        if (categoryDTO.getName()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        Category category=new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        category.setStatus(StatusConstant.DISABLE);

        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);

        return Result.success();
    }


    /**
     * 分类分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO dto) {
        if (dto.getPage()==null){
            dto.setPage(1);
        }
        if (dto.getPageSize()==null){
            dto.setPageSize(10);
        }
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<Category> pageParam =  categoryMapper.pageQuery(dto);

        PageResult pageResult=new PageResult();
        pageResult.setTotal(pageParam.getTotal());
        pageResult.setRecords(pageParam.getResult());

        return Result.success(pageResult);
    }


    /**
     * 根据ID删除分类
     *
     * @param id
     * @return
     */
    @Override
    public Result<Object> deleteClassificationById(Long id) {
        if (id==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        categoryMapper.deleteById(id);

        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param dto
     * @return
     */
    @Override
    public Result modifyClassification(CategoryDTO dto){
        if(dto.getId()==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Category category=new Category();
        BeanUtils.copyProperties(dto,category);

        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());

        categoryMapper.modify(category);

        return Result.success();
    }

    /**
     * 分类启用禁用
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result updateCategoryStatus(Integer status, Long id) {
        if (id==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        if (status==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.modify(category);

        return Result.success();
    }

    /**
     * 类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> queryList(Integer type) {
        return categoryMapper.list(type);
    }


}
